package com.etrans.web.gps.business.alarm.service;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.alarm.domain.AlarmDealInfo;
import com.etrans.web.gps.business.alarm.domain.AlarmHistoryInfo;
import com.etrans.web.gps.business.alarm.domain.AlarmHourStatistics;
import com.etrans.web.gps.business.alarm.domain.AlarmPenddingInfo;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.EgovUtil;
import com.etrans.web.gps.util.StringUtils;

/**
 * <p>
 * Title: AlarmController.java<／p>
 * <p>
 * Description:实时报警处理service <／p>
 * <p>
 * Copyright: Copyright (c) 2007<／p>
 * <p>
 * Company: E-trans<／p>
 * 
 * @author lxd
 * @date 2017年6月17日
 * @time 下午2:58:33
 * @version 1.0
 */
@Service
public class AlarmService extends BaseService {

	@Autowired
	private EgovUtil egovUtil;
	
	/**
	 * 查询当天报警分时段统计信息
	 * 
	 * @param userId
	 * @return
	 */
	public List<AlarmHourStatistics> getAlarmByHourGroupStatistics(Map<String, Object> param) {
		List<AlarmHourStatistics> listOperation = this
				.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.AlarmGroupStatisticsOperation", param);
		List<AlarmHourStatistics> listAllDay = this
				.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.AlarmGroupStatisticsAllDay", param);
		for (AlarmHourStatistics allDay : listAllDay){
			for (AlarmHourStatistics operation : listOperation){
				if (allDay.getHour().equals(operation.getHour())){
					Set set = new HashSet(Arrays.asList(allDay.getVehicleIds().split(",")));
					Set set1 = new HashSet(Arrays.asList(operation.getVehicleIds().split(",")));
					set.addAll(set1);
					allDay.setCount(set.size());
					listOperation.remove(operation);
					break;
				}
			}
			allDay.setVehicleIds(null);
		}
		listAllDay.addAll(listOperation);
		Collections.sort(listAllDay);
		return listAllDay;
	}

	/**
	 * 查询待处理报警
	 */
	public PageVo getAlarmPendingInfo(Map<String, Object> param) {
		List<AlarmPenddingInfo> list = this
				.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getAlarmPendingInfo", param);
		Integer totalRow = this.selectOne("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getAlarmPendingCount",param);
		
		PageVo pageVo = new PageVo();
		pageVo.setRows(list);
		pageVo.setTotal(totalRow);
		pageVo.setPage((Integer) param.get("page"));
		return pageVo;
	}

	/**
	 * 查询待处理报警明细
	 * 
	 */
	public List<AlarmPenddingInfo> getAlarmPendingDetail(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return this.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getAlarmPendingDetail", param);
	}

	/**
	 * 新增处警记录
	 * 
	 * @param alarmDeal
	 */
	public void handerAlarm(AlarmDealInfo alarmDeal) {
		this.insert("com.etrans.web.gps.business.alarm.mapper.alarmMapper.saveAlarmDealInfo", alarmDeal);
	}

	/**
	 * 修改实时报警表处警字段
	 * 
	 * @param alarmDeal
	 */
	public void alterRealTimeAlarm(AlarmDealInfo alarmDeal) {
		this.insert("com.etrans.web.gps.business.alarm.mapper.alarmMapper.alterRealTimeAlarm", alarmDeal);
	}
	
	
	
	/**
	 * 删除实时报警表到历史表
	 * 
	 * @param alarmDeal
	 */
	public void deleteRealTimeAlarm(AlarmDealInfo alarmDeal) {
		this.insert("com.etrans.web.gps.business.alarm.mapper.alarmMapper.deleteRealTimeAlarm", alarmDeal);
	}

	/**
	 * 移动报警数据至历史表
	 * 
	 * @param alarmDeal
	 */
	public void moveAlarmToHistory(AlarmDealInfo alarmDeal) {
		this.insert("com.etrans.web.gps.business.alarm.mapper.alarmMapper.moveAlarmToHistory", alarmDeal);
	}

	/**
	 * 实时报警处理业务逻辑 1. 新增 alarm_dealinfo表的记录 2.
	 * 修改alarm_info_operation的dealer_id关联刚新增字段 3. 把该车，该报警类型的当天的业务数据全部迁移到历史表
	 * 
	 * @param param
	 * @throws Exception 
	 */
	public void saveHandler(Set<Integer> alarmKindIds,AlarmDealInfo alarmDeal) throws Exception {
		HashMap<String, Object> param =new HashMap<>();
		param.put("vehicleId", alarmDeal.getVehicleId());
		String terminalCommnNo = hasRealTimeAlarm(param);
		handerAlarm(alarmDeal);

		alterRealTimeAlarm(alarmDeal);

		moveAlarmToHistory(alarmDeal);
		
		deleteRealTimeAlarm(alarmDeal);
		
		clearAlarm(alarmKindIds,terminalCommnNo);
		
	}

	public void clearAlarm(Set<Integer> alarmKindIds,String commno) throws Exception{
		String[] commnos = new String[]{commno};
		this.clearAlarm(commnos);
		this.clearAlarmT(alarmKindIds, commnos);
	}
	/**
	 * 处理报警
	 * @throws Exception 
	 */
	public void clearAlarm(String[] commnos) throws Exception{
//		egovUtil.clearAlarm(commnos);
	}
	/**
	 * 处理报警
	 * @throws Exception 
	 */
	public void clearAlarmT(Set<Integer> alarmKindIds,String[] commnos) throws Exception{
//		egovUtil.clearAlarmT(alarmKindIds, commnos);
	}
	/**
	 * 历史报警处理业务逻辑 1. 新增 alarm_dealinfo表的记录 2.
	 * 修改alarm_info_operation的dealer_id关联刚新增字段 3. 把该车，该报警类型的当天的业务数据全部迁移到历史表
	 * 
	 * @param param
	 */
	public void saveHisHander(AlarmDealInfo alarmDeal, HashMap<String, Object> param) {

		alarmDeal.setAlarmType(Integer.parseInt(StringUtils.nullToEmptyOfObject(param.get("alarmType"))));
		alarmDeal.setVehicleId(Integer.parseInt(StringUtils.nullToEmptyOfObject(param.get("vehicleId"))));
		alarmDeal.setAlarmTime(StringUtils.nullToEmptyOfObject(param.get("alarmTime")));
		// 插入处警信息
		handerAlarm(alarmDeal);
		// 更新报警数据
		alterHistoryAlarm(alarmDeal);

	}

	/**
	 * 修改实时报警表处警字段
	 * 
	 * @param alarmDeal
	 */
	public void alterHistoryAlarm(AlarmDealInfo alarmDeal) {
		this.insert("com.etrans.web.gps.business.alarm.mapper.alarmMapper.alterHistoryAlarm", alarmDeal);
	}

	/**
	 * 查询车辆是否还有报警
	 */
	public String hasRealTimeAlarm(HashMap<String, Object> param) {
		return this.selectOne("com.etrans.web.gps.business.alarm.mapper.alarmQuery.hasRealTimeAlarm", param);
	}

	/**
	 * 查询当天报警分时段统计报警的明细数据
	 */
	public List<AlarmPenddingInfo> getAlarmByHourDetailStatistics(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return this.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.AlarmByHourDetailStatistics",
				param);
	}

	/**
	 * 查询当天报警分时段统计报警的明细数据
	 */
	public Integer getAlarmByHourDetailStatisticsCount(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return this.selectOne("com.etrans.web.gps.business.alarm.mapper.alarmQuery.AlarmByHourDetailStatisticsCount",
				param);
	}

	public List queryAlarmByCondition(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return this.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getAlarmType", param);
	}

	/***
	 * 历史报警分表查询逻辑
	 * 	1. 如果查询日期隔月,分两次查询
	 *   前端展示按照时间倒序，因此后面的日期先查询,如果条数不满分页数据，则进行第二次查询
	 *  2. 总数必须查询两次,进行汇总
	 */
	public PageVo getHistoryAlarm(Map<String, Object> param) {
		PageVo pageVo = new PageVo();
		
		String start = param.get("startDate").toString().substring(0, 7).replace("-", "_");
		String end = param.get("endDate").toString().substring(0, 7).replace("-", "_");
		List<AlarmHistoryInfo> list = null;
		Integer total = null;
		if(start.equals(end)){//同月份查询
			param.put("table_name", start);
			list = this.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getHistoryAlarm", param);
			total = this.selectOne("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getHistoryAlarmCount",param);
			pageVo.setTotal(total);
			pageVo.setRows(list);
		}else{
			param.put("table_name", end);
			list = this.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getHistoryAlarm", param);
			total = this.selectOne("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getHistoryAlarmCount",param);
			pageVo.setTotal(total);
			//数据不够分页 
			if(list.size() < Integer.parseInt(param.get("pageSize").toString())){
				param.put("table_name", start);
				param.put("pageSize", Integer.parseInt(param.get("pageSize").toString()) - list.size());
				List<AlarmHistoryInfo> sublist = this.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getHistoryAlarm", param);
				list.addAll(sublist);
			}
			pageVo.setRows(list);
			param.put("table_name", start);
			total = this.selectOne("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getHistoryAlarmCount",param);
			pageVo.setTotal(total + pageVo.getTotal());
		}
		pageVo.setPage((Integer) param.get("page"));
		return pageVo;
	}

	public List<AlarmPenddingInfo> getAlarmPendingDetailInfo(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		List<AlarmPenddingInfo> list = this
				.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getAlarmPendingDetailInfo", param);
		return list;

	}
	
	/**
	 * 
	 * **/
	public String getVehicleInfo(Integer vehicleId) {
		return this.selectOne("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getVehicleInfo", vehicleId);
	}

	/**
	 * 查询车辆静态数据
	 * @param userId
	 * @return
	 */
	public List<HashMap<String, String>> getVehStaticInfo(HashMap<String, Object> param) {
		return this.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.getVehStaticInfo", param);
	}
	
	/**
	 * 查询报警类型
	 */
	public List<HashMap<String, Object>> queryAlarmType(Map<String, Object> params) {
		return  this.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.queryAlarmType", params);
	}
	
	public String queryAlarmTypeById(int id) {
		return this.selectOne("com.etrans.web.gps.business.alarm.mapper.alarmQuery.queryAlarmTypeById", id);
	}
	
	public Map<Integer, String> getAlarmTypeMap() {
		HashMap<Integer, String> maps = new HashMap<>();
			List<HashMap<String, Object>> alarmTypeList = this.selectList("com.etrans.web.gps.business.alarm.mapper.alarmQuery.queryAlarmType", null);
			for (HashMap<String, Object> hashMap : alarmTypeList) {
				maps.put(Integer.valueOf(String.valueOf(hashMap.get("id"))), String.valueOf(hashMap.get("name")));
			}
		return maps;
	}
	

}
